package com.daocao.yfb.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.daocao.yfb.R;
import com.daocao.yfb.adapter.MyOrderAdapter;
import com.daocao.yfb.api.Api;
import com.daocao.yfb.api.bean.PageBean;
import com.daocao.yfb.api.bean.UpdateAccountBean;
import com.daocao.yfb.api.model.OrderListModel;
import com.daocao.yfb.base.BaseActivity;
import com.daocao.yfb.event.SelectAccountEvent;
import com.daocao.yfb.util.Network;
import com.daocao.yfb.util.RxLifeUtil;
import com.daocao.yfb.util.ToastUtil;
import com.daocao.yfb.view.OnLoadeMoreListener;
import com.daocao.yfb.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyOrderActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;

    private Unbinder unbiner;
    private MyOrderAdapter adapter;
    private OnLoadeMoreListener listener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getOrderList(page);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_my);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        EventBus.getDefault().register(this);
        txtTitle.setText("我的订单");
        adapter = new MyOrderAdapter(this);
        list.setAdapter(adapter);
        list.addOnScrollListener(listener);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                listener.setPage(1);
                getOrderList(1);
            }
        });
        getOrderList(1);
    }

    public void getOrderList(int page) {

        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        refresh.setRefreshing(true);
        PageBean bean = new PageBean();
        bean.page = page;
        Api.api_service.getOrderList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setData(result.datas, page);
                });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(SelectAccountEvent event) {
        if (TextUtils.isEmpty(event.id)) {
            return;
        }
        getUpdateAccount(event.order_number, event.id);
    }

    public void getUpdateAccount(String order_number, String relate_id) {

        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        UpdateAccountBean bean = new UpdateAccountBean();
        bean.order_number = order_number;
        bean.relate_id = relate_id;
        Api.api_service.getUpdateAccount(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "修改成功");
                    listener.setPage(1);
                    getOrderList(1);
                });

    }

    private void setData(List<OrderListModel.DatasBean> datas, int page) {
        adapter.add(datas, page == 1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }
}
